package com.along.utility.page.mybatis;

import com.github.pagehelper.PageHelper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;


@Aspect
public class PagingAspect {

	@Around("execution(public * *(..)) && @annotation(com.along.utility.page.mybatis.Paging)")
	public Object page(ProceedingJoinPoint pjp) throws Throwable {
		Throwable throwable = null;
		Object[] args = pjp.getArgs();
		if(args.length<2){
			throw new Throwable("page method parameter number is not correct.");
		}
		try {
			int pageNum = (Integer) args[args.length-2];
			int pageSize = (Integer) args[args.length-1];
			PageHelper.startPage(pageNum,pageSize);
			return pjp.proceed();
		} catch (Throwable t) {
			throwable =  t;
		}
		throw throwable;
	}
}
